package cn.magictodo.weblog.admin.event.subscriber.redis;

import cn.magictodo.weblog.admin.event.PublishArticleEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import static cn.magictodo.weblog.common.enums.RedisPrefixEnum.REDIS_IS_PUBLISH;

/**
 * 文章发布事件监听者.处理redis中文章的发布状态
 *
 * @author liu
 * @since 2023-12-22 13:44
 */
@Component
@Slf4j
public class IsPublishArticleSubscriber implements ApplicationListener<PublishArticleEvent> {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Async("threadPoolTaskExecutor")
    @Override
    public void onApplicationEvent(PublishArticleEvent event) {
        Long articleId = event.getArticleId();
        redisTemplate.opsForValue().set(REDIS_IS_PUBLISH.getPrefix() + articleId, true);
        log.info("REDIS ==> 文章推送事件消费成功，articleId: {}", articleId);
    }
}
